package com.gdth.log;

import java.io.File;
import java.text.ParseException;
import java.util.Comparator;
import java.util.Date;

public class FileComparator implements Comparator<File> {
	
	public FileComparator(){
		
	}
	
    public int compare(File file1, File file2) {
        if (LogUtil.logServiceLogName.equals(file1.getName())) {
            return -1;
        } else if (LogUtil.logServiceLogName.equals(file2.getName())) {
            return 1;
        }

        String createInfo1 = LogUtil.getFileNameWithoutExtension(file1.getName());
        String createInfo2 = LogUtil.getFileNameWithoutExtension(file2.getName());

        try {
            Date create1 = LogUtil.sdf.parse(createInfo1);
            Date create2 = LogUtil.sdf.parse(createInfo2);
            if (create1.before(create2)) {
                return -1;
            } else {
                return 1;
            }
        } catch (ParseException e) {
            return 0;
        }
    }
}
